﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation;
using System.Reflection;
using System.Diagnostics;
using CH.Easy.Log;
namespace CH.Easy.Aop.Test
{
    #region 拦截器
    public class LogBefore : BeforeAspectAttribute
    {
        public override void Excute(InvokeMessage msg)
        {
            msg.Message = "开始执行";
            Loger.Log("开始执行");
        }
    }
    public class LogAfter : AfterAspectAttribute
    {
        public override void Excute(InvokeMessage msg)
        {
            Loger.Log("结束执行");
        }
    }
    public class LogExption : ExceptionAspectAttribute
    {
        public override void Excute(InvokeMessage msg)
        {
            Loger.Log(msg.Exception, "程序异常");
            msg.Result = 19;
        }
    }





    public class PermissionBefore : BeforeAspectAttribute
    {
        public override void Excute(InvokeMessage msg)
        {
            
            //msg.Message = "没有通过权限验证";
            //msg.Result = -1;
        }
    }
    #endregion


    public class Photo
    {
        public virtual int ID { get; set; }
        public virtual string Title { get; set; }
        public virtual DateTime AddTime { get; set; }
    }

    public class PhotoLogic
    {
       
        [PermissionBefore, LogExption, LogAfter, LogBefore]
        public virtual int UpdatePhotoTitle(string title, int pid)
        {

            //记录开始
            // throw new ArgumentException("参数异常");
            //记录结束
            return 22;
        }


        //[PermissionBefore, LogExption, LogAfter, LogBefore]
        public virtual Photo AddPhoto(Photo p)
        {
            //throw new ArgumentException("参数异常");
            return p;
        }
    }




 





















    class Program
    {
        static void Main(string[] args)
        {
            PhotoLogic proxy = ProxyFactory<PhotoLogic>.CreateAndSave();
            proxy.UpdatePhotoTitle("fdsa", 0);




            //Stopwatch t = new Stopwatch();
            //ProxyFactory<PhotoLogic>.Create();
            //Console.WriteLine("测试实例化1千万个对象");

            //List<PhotoLogic> list = new List<PhotoLogic>();

            //t.Start();
            //for (int i = 0; i < 10000000; i++)
            //{
            //    list.Add(ProxyFactory<PhotoLogic>.Create());
            //}
            //Console.WriteLine("使用AOP代理方式 ProxyFactory<PhotoLogic>.Create() 耗时:" + t.ElapsedMilliseconds + "毫秒");
            //t.Stop();
            //list = new List<PhotoLogic>();
            //t.Reset();
            //t.Start();

            //for (int i = 0; i < 10000000; i++)
            //{
            //    list.Add(new PhotoLogic());
            //}


            //t.Stop();
            //Console.WriteLine("正常实例化 new PhotoLogic() 耗时:" + t.ElapsedMilliseconds + "毫秒");







            //Console.WriteLine("调用对象10000次UpdatePhotoTitle方法");
            //t.Reset();
            //t.Start();
            //PhotoLogic proxy = ProxyFactory<PhotoLogic>.Create();
            //for (int i = 0; i < 100000; i++)
            //{
            //    proxy.UpdatePhotoTitle("fdsa", 0);
            //}

            //Console.WriteLine("使用AOP代理方式 ProxyFactory<PhotoLogic>.Create() 耗时:" + t.ElapsedMilliseconds + "毫秒");
            //t.Stop();


            //t.Reset();
            //t.Start();
            //PhotoLogic pl = new PhotoLogic();
            //for (int i = 0; i < 100000; i++)
            //{
            //    pl.UpdatePhotoTitle("", 0);
            //}

            //Console.WriteLine("正常实例化 new PhotoLogic() 耗时:" + t.ElapsedMilliseconds + "毫秒");
            //t.Stop();







           
            Console.Read();
        }
    }
}
